<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * Core Modular System / Interface
 * Started: August 27, 2009 12:10:52 PM
 * Script version: 1.1
 +------------------------------------
 */
interface module_interface{
	public function returninitvars();
	public function construct();
	public function run();
	public function destruct();
}

class modular_system{
	public function __construct($interface, $application, $module){
		global $perm;
		$this->module_name = str_replace("/", "", $module);
		if (IN_WINDOWS) $this->module_name = str_replace("\\", "", $module);
		$this->application_name = str_replace("/", "", $application);
		if (IN_WINDOWS) $this->application_name = str_replace("\\", "", $application);
		if(strcmp($interface, "frontend") == 0){
			$this->module_path = realpath(FRONTEND_PATH.$this->application_name."/modules")."/";
			$this->module_complete_path = $this->module_path.$this->module_name.".php";
			if(!file_exists($this->module_complete_path)){
				trigger_error("Frontend Module Error: {$this->module_name} located in application {$this->application_name} couldn't be found.");
			}
		}
		elseif(strcmp($interface, "backend") == 0){
			$this->module_path = realpath(BACKEND_PATH.$this->application_name."/modules")."/";
			$this->module_complete_path = $this->module_path.$this->module_name.".php";
			if(!file_exists($this->module_complete_path)){
				trigger_error("Backend Module Error: {$this->module_name} located in application {$this->application_name} couldn't be found.");
			}
		}
		else{
			trigger_error("Interface {$interface} isn't a valid interface (frontend / backend).");
		}
		require_once($this->module_complete_path);
		if(class_exists("module") !== false && ($this->module = new module) !== false){
			$this->module->construct();
			$this->moduleInfo = $this->module->returninitvars();
			$this->moduleTitle = $this->moduleInfo["page_title"];
			$this->moduleDesc = $this->moduleInfo["page_description"];
			if(!isset($this->module->run)){
				$this->module->run();
			}
			elseif($this->module->run === true) $this->module->run();
			else{
				if(!isset($this->module->custom_error)) $perm->noPermissionError();
				elseif($this->module->custom_error === false) $perm->noPermissionError();
			}
			$this->module->destruct();
		}
		else trigger_error("Couldn't load module {$this->module_name} located in application {$this->application_name}.");
	}
	
	public function getApplicationName(){
		return $this->application_name;
	}
	
	public function getModuleName(){
		return $this->module_name;
	}
	
	public function getModulePath(){
		return $this->module_path;
	}
	
	public function getModuleTitle(){
		return $this->moduleTitle;
	}
	
	public function getModuleDesc(){
		return $this->moduleDesc;
	}
}
